package xgenerator.ui.swing;

import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.event.ComponentAdapter;
import java.awt.event.ComponentEvent;
import java.net.URL;

import javax.swing.ImageIcon;
import javax.swing.JPanel;

public class BackgroundImagePanel extends JPanel {

	private static final long serialVersionUID = 1L;
	
	public static final int VERTICAL_TOP = 1; // 垂直向上

	public static final int VERTICAL_MIDDLE = 2; // 垂直居中

	public static final int VERTICAL_BOTTOM = 3; // 垂直向下

	public static final int HORIZONTAL_LEFT = 4; // 水平向左

	public static final int HORIZONTAL_CENTER = 5; // 水平居中

	public static final int HORIZONTAL_RIGHT = 6; // 水平向右
	
	/**
	 * 
	 */
	private int imageWidth;
	private int imageHeight;
	private String imagePath;
	private int align;
	private int valign;
	private Color bgcolor;

	/**
	 * constructor
	 * @param imagePath
	 */
	public BackgroundImagePanel(String imagePath) {
		this(imagePath, null);
	}
	
	/**
	 * constructor
	 * @param imagePath
	 * @param bgcolor
	 */
	public BackgroundImagePanel(String imagePath, Color bgcolor) {
		this(imagePath, HORIZONTAL_CENTER, VERTICAL_MIDDLE, bgcolor);
	}
	
	/**
	 * constructor
	 * @param imagePath
	 * @param align
	 * @param valign
	 * @param bgcolor
	 */
	public BackgroundImagePanel(String imagePath, int align, int valign, Color bgcolor) {
		this(imagePath, align, valign, 0, 0, bgcolor);
	}
	
	/**
	 * constructor
	 * @param imagePath
	 * @param align
	 * @param valign
	 * @param width
	 * @param hight
	 */
	public BackgroundImagePanel(String imagePath, int align, int valign, int width, int hight) {
		this(imagePath, align, valign, width, hight, null);
	}
	
	/**
	 * constructor
	 * @param imagePath
	 * @param align
	 * @param valign
	 * @param width
	 * @param hight
	 * @param bgcolor
	 */
	public BackgroundImagePanel(String imagePath, int align, int valign, int width, int hight, Color bgcolor) {
		this.imagePath = imagePath;
		this.align = align;
		this.valign = valign;
		this.imageWidth = width;
		this.imageHeight = hight;
		this.bgcolor = bgcolor;
		
		/*
		this.addComponentListener(new ComponentAdapter() {
			@Override
			public void componentResized(ComponentEvent e) {
				super.componentResized(e);
				repaint(getHorizontalX(), getVerticalY(), imageWidth, imageHeight);
				System.out.println("---------------------------");
				paintComponent(getGraphics());
			}
		});
		*/
	}

	/**
	 * 重写paintComponent方法
	 * @author <a href="mailto:shushanlee@msn.com">liss</a>
	 * @param g
	 * @see javax.swing.JComponent#paintComponent(java.awt.Graphics)
	 */
	protected void paintComponent(Graphics g) {
		Graphics2D g2 = (Graphics2D) g;
		
		if(0 == this.imageWidth) {
			this.imageWidth = this.getWidth();
		}
		if(0 == this.imageHeight) {
			this.imageHeight = this.getHeight();
		}
		
		Image img = IconUtils.createIconImage(imagePath);
		if(null != this.bgcolor) {
			g2.drawImage(img, getHorizontalAlignmentX(), getVerticalAlignmentY(), this.imageWidth, this.imageHeight, this.bgcolor, this);
		} else {
			g2.drawImage(img, getHorizontalAlignmentX(), getVerticalAlignmentY(), this.imageWidth, this.imageHeight, this);
		}
	}
	
	/**
	 * <p>
	 * Title：水平X坐标
	 * </p>
	 * @author <a href="mailto:shushanlee@msn.com">liss</a>
	 * @return
	 */
	private int getHorizontalAlignmentX() {
		if(HORIZONTAL_LEFT == this.align) {
			return 0;  //水平居左坐标
		} else if(HORIZONTAL_CENTER == this.align) {
			return (this.getWidth() - this.imageWidth) / 2;  //水平居中坐标
		} else if(HORIZONTAL_RIGHT == this.align) {
			return this.getWidth() - this.imageWidth;  //水平居右坐标
		} else {
			return 0;  //水平居左坐标
		}
	}
	
	/**
	 * <p>
	 * Title：垂直Y坐标
	 * </p>
	 * @author <a href="mailto:shushanlee@msn.com">liss</a>
	 * @return
	 */
	private int getVerticalAlignmentY() {
		if(VERTICAL_TOP == this.valign) {
			return 0;  //水平顶部坐标
		} else if(VERTICAL_MIDDLE == this.valign) {
			return (this.getHeight() - this.imageHeight) / 2;  //水平中部坐标
		} else if(VERTICAL_BOTTOM == this.valign) {
			return this.getHeight() - this.imageHeight;  //水平底部坐标
		} else {
			return 0;  //水平顶部坐标
		}
	}

}
